#!/bin/sh
# This regression test is a part of SIPp.
# With -max_reconnect, a call whose TLS connection is refused fails and
# the others go on; SIPp stopped with a fatal error instead. Without it,
# a refused connection is still fatal.
. "`dirname "$0"`/../functions"; init

"`get_sipp`" -v | grep -q TLS || skip "no TLS"
command -v openssl >/dev/null || skip "no openssl"

certs=`mktemp -d`
trap 'rm -rf "$certs"' EXIT
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \
    -nodes -keyout "$certs/key.pem" -out "$certs/cert.pem" -days 1 \
    -subj /CN=sipp >/dev/null 2>&1 || fail "cannot create the certificate"

# Nothing listens on 26611.
uac="-sn uac -t ln -i 127.0.0.1 -p 26610 -m 3 -timeout 5
    -tls_cert $certs/cert.pem -tls_key $certs/key.pem 127.0.0.1:26611"
sippfg $uac -max_reconnect 5 >/dev/null 2>&1
reconnect=$?
sippfg $uac >/dev/null 2>&1
default=$?

if test $reconnect -ne 1; then
    fail "with -max_reconnect, ended with $reconnect, not 1"
elif test $default -ne 255; then
    fail "without -max_reconnect, ended with $default, not 255"
else
    ok
fi
